
package Persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import Controlador.Utils;
import Negocio.*;



public class AdmPersistenciaCerealCosechaFina extends AdministradorPersistencia 
{
	private static AdmPersistenciaCerealCosechaFina instancia;
	
	private AdmPersistenciaCerealCosechaFina()
	{
		
	}
	public static AdmPersistenciaCerealCosechaFina getInstancia()
	{
		if (instancia == null)
			instancia = new AdmPersistenciaCerealCosechaFina();
		return instancia;
	}

	public void borrar(Object d) 
	{
		System.out.println("LOG: AdmPersistenciaCerealCosechaFina / Delete [INI]");
		try
		{
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("update  cereal set estado = ? where codigo = ?");
			s.setString(1, Utils.CONSTANTE_INACTIVO);
			s.setLong(2, (Long) d);
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
			System.out.println("LOG: AdmPersistenciaCerealCosechaFina / Delete [FIN]");
		}
		catch (Exception e)
		{
			// TODO Exception
			System.out.println("No se puede elminiar el elemento");
		}
		

	}

	@Override
	public Long insertar(Object o) 
	{
		System.out.println("LOG: AdmPersistenciaCerealCosechaFina / insert [INI]");
		try
		{
			Long res = null;
			CerealCosechaFina a = (CerealCosechaFina)o;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("insert into cereal values (?,?,?,?,?,?,?)");
			
			//agregar campos
			
			s.setString(1, a.getNombre());
			s.setInt(2, a.getInicioEpocaSiembra());
			s.setLong(3,a.getMineral().getCodigo());
			s.setInt(4, a.getFinEpocaSiembra());
			s.setDouble(5,a.getRinde());
			s.setInt(6, Utils.CONSTANTE_COSECHAFINA);
			s.setString(7,a.getEstado());
			
			s.execute();
			
			s = con.prepareStatement("select max(codigo) from cereal");
			ResultSet result = s.executeQuery();
			
			if (result.next())
				res = result.getLong(1);
			PoolConnection.getPoolConnection().realeaseConnection(con);
			System.out.println("LOG: AdmPersistenciaCerealCosechaFina / insert [FIN]");
			return res;
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		}
		return null;

	}

	@Override
	public Vector<Object> buscarTodos() 
	{
		System.out.println("LOG: AdmPersistenciaCerealCosechaFina / select [INI]");
		//TODO Hacer funcionalidad
		System.out.println("LOG: AdmPersistenciaCerealCosechaFina / select [FIN]");
		return null;
	}


	@Override
	public Object buscarPorCodigo(Object o) {
		System.out.println("LOG: AdmPersistenciaCerealCosechaFina / findById [INI]");
		
		try {
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s;
			
				s = con.prepareStatement("select * from  cereal where desperdicio = 0 and codigo = ?");
			
					//agregar campos
			s.setLong(1,(Long)o);
			ResultSet result = s.executeQuery();
			CerealCosechaFina a = null;
			
			if (result.next()){
				Mineral m = (Mineral) AdmPersistenciaMineral.getInstancia().buscarPorCodigo(result.getLong(4));
				a = new CerealCosechaFina(
											result.getLong(1),
											result.getString(2),
											result.getInt(3),
											result.getInt(5),
											result.getDouble(6),
											result.getString(8),
											m
										  );
							
			} else
			{
				//TODO Exception
				System.out.println("No se encuentra el Cereal cosecha Gruesa");
				
			}
			
			PoolConnection.getPoolConnection().realeaseConnection(con);
			System.out.println("LOG: AdmPersistenciaCerealCosechaFina / findById [FIN] ");
			
			return a;
			
	} catch (SQLException e) {
			
			//TODO Exception
			System.out.println("Error extraccion");
			return null;
		
	}
		

	}

	public Object buscarPorNombre(Object o) {

		try
		{
			CerealCosechaFina a = null;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select * from  cosechaGruesa where nombre = ?");
			s.setString(1,(String) o);
			ResultSet result = s.executeQuery();
			if (result.next())
			{
				if(result.getDouble(7)== 0){
	
					
					Mineral m = (Mineral) AdmPersistenciaMineral.getInstancia().buscarPorCodigo(result.getLong(4));
					a = new CerealCosechaFina(
												result.getLong(1),
												result.getString(2),
												result.getInt(3),
												result.getInt(5),
												result.getDouble(6),
												result.getString(7),
												m
											  );
				}else{
					
					System.out.println("El Cereal es de cosecha Gruesa");
				
				}
			}else{
				//TODO Exception
				System.out.println("No se encuentra el Cereal cosecha Fina");
				return null;
			}
			
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return a;
		}
		catch (Exception e)
		{
			//TODO Exception
			System.out.println("Error extraccion");
			return null;
		}
	}
	@Override
	
	public void modificar(Object o) {
		System.out.println("LOG: AdmPersistenciaCerealCosechaFina / update [INI]");
		try
		{
			CerealCosechaFina a = (CerealCosechaFina)o;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("update cereal set nombre = ?,inicioEpoca = ?,finEpoca=?,rinde = ?,estado=	? where codigo = ?" );
			
			//agregar campos
			s.setString(1, a.getNombre());
			s.setInt(2, a.getInicioEpocaSiembra());
			s.setInt(3, a.getFinEpocaSiembra());
			s.setDouble(4, a.getRinde());
			s.setString(5, a.getEstado());
			s.setLong(6, a.getCodigo());
			
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
			System.out.println("LOG: AdmPersistenciaCerealCosechaFina / update [FIN]");
		}
		catch (SQLException e)
		{
			//TODO Exception
			System.out.println(e.getMessage());
		}
		
	}
	public Vector<CerealCosechaFina> selectAll() {
		try
		{
			Vector<CerealCosechaFina> a = new Vector<CerealCosechaFina>();
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select * from cereal");
			ResultSet result = s.executeQuery();
		    while (result.next())
			{
				if(result.getDouble(7)== 0){
	
					Mineral m = (Mineral) AdmPersistenciaMineral.getInstancia().buscarPorCodigo(result.getLong(4));
					a.add(new CerealCosechaFina(
												result.getLong(1),
												result.getString(2),
												result.getInt(3),
												result.getInt(5),
												result.getDouble(6),
												result.getString(7),
												m
											  ));

				}
			}
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return a;
		}
		catch (Exception e)
		{
			//TODO Exception
			System.out.println("Error extraccion");
			return null;
		}
	}
	

}
